#!/usr/bin/perl
use modules::encode;
use strict;
package csremove;

sub csremove {
    my ($dbh,$remote,$input,$csnum)=@_;
    my $sth; my $auth; my ($level1,$level2);
    if ($$input[5] eq "") {
	print $remote "$csnum O $$input[0] :Syntax :REMOVE <#channel> <rnick>\n";
    }
    else {
	$sth=$dbh->prepare("SELECT auth FROM ninfo WHERE code = '".encode::encode($$input[0])."' AND num = '$$input[0]';");
	$sth->execute;
	$auth = $sth->fetchrow_array;
	if (!$auth) {
	    print $remote "$csnum O $$input[0] :Access denied.(Not identified)\n";
	}
	else {
	    $sth=$dbh->prepare("SELECT level FROM users WHERE rnick = '$auth' AND channel = '$$input[4]' AND level > 449;");
	    $sth->execute;
	    $level2 = $sth->fetchrow_array;
	    if (!$level2) {
		print $remote "$csnum O $$input[0] :Access denied. (Insufficient access)\n";
	    }
	    else {
		$sth=$dbh->prepare("SELECT level FROM users WHERE rnick = '$$input[5]' AND channel = '$$input[4]';");
		$sth->execute;
		if (!($level1 = $sth->fetchrow_array)) {
		    print $remote "$csnum O $$input[0] :No such user.\n";
		}
		else {
		    if ($level2 <= $level1) {
			print $remote "$csnum O $$input[0] :Tak Boleh! (Cannot remove your equals or your seniors).\n";
		    }
		    else {
			print $remote "$csnum O $$input[0] :Removed $$input[5] from $$input[4].\n";
			$sth=$dbh->prepare("DELETE FROM users WHERE rnick = '$$input[5]' AND channel = '$$input[4]';");
			$sth->execute;
		    }
		}
	    }
	}
    }
}



1;
